import axios from 'axios'
import setState from './setState'
const instance = axios.create({
  baseURL: '',
  timeout: 5000
})

instance.interceptors.request.use(req => {
  console.log('进入请求拦截器，请求的配置如下')
  console.log(req.data)
  console.log(req)
  return req
}, err => {
  console.log('进入请求拦截器失败')
  console.log(err)
})
instance.interceptors.response.use(res => {
  console.log('进入响应应拦截器')
  switch (res.status) {
    case 200:
      console.log('状态码200:请求成功')
      setState('enter/message', res.data)
      console.log('res.data', res.data)
      break
    case 404:
      console.log('状态码为404:找不到资源')
      break
    case 500:
      console.log('状态码为500:找不到服务器')
      break
  }
  console.log(res)
  return res
}, err => {
  console.log('进入响应拦截器失败', err)
})

export default (data, url) => {
  return instance({method: 'GET', url: url, params: data})
  // .then(function (response) {
  //   console.log('请求')
  //   console.log('enter/message', response.data.msg)
  //   setState('enter/message', response.data.msg)
  //   console.log(this.$store.state.enter.message)
  // }).catch(function (response) {
  //   console.log('请求失败')
  //   console.log(response)
  // })
}
